//
//  QDSettingController.m
//  simulateQuickD/Users/yaohuajie/Desktop/HJ-Y/simulateQuickDoctor/simulateQuickDoctor/simulateQuickDoctor/Classes/Home(主页)/SlideMenu(左侧菜单)/Setting(设置与帮助)/View/QDSettingCell.moctor
//
//  Created by 姚华杰 on 2017/3/23.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDSettingController.h"
#import "QDSettingCell.h"
#import "QDFeedbackController.h"
#import "QDSettingCell.h"
#import "QDSettingAndHelpTableViewModel.h"
#import "QDBannerTools.h"

@interface QDSettingController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *taBleView;

//数据源
@property(nonatomic,strong) NSMutableArray *arrList;

@property(nonatomic,assign) float fileSize;



@end

@implementation QDSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    _arrList = [NSMutableArray array];
    
    self.arrList = [QDSettingAndHelpTableViewModel getSettingModelArr];
    
}




- (void)setupUI
{
    
    
    
    //设置预估行高和真实行高
    _taBleView.estimatedRowHeight = 200;
    _taBleView.rowHeight = UITableViewAutomaticDimension;
    
    //注册cell
    [_taBleView registerNib:[UINib nibWithNibName:@"QDSettingCell" bundle:nil] forCellReuseIdentifier:@"Setting"];
    
    //设置代理
    _taBleView.dataSource = self;
    _taBleView.delegate = self;
    
    self.navigationItem.title = @"设置与帮助";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QDSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Setting" forIndexPath:indexPath];

    
    QDSettingAndHelpTableViewModel *model = _arrList[indexPath.row];
    cell.model = model;

    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *docSB = [UIStoryboard storyboardWithName:@"QDFeedback" bundle:nil];
    
    QDFeedbackController *feedVC = [docSB instantiateViewControllerWithIdentifier:@"Feedback"];
    
    switch (indexPath.row) {
        case 0:
            //意见反馈的跳转
            feedVC.navigationItem.title = @"意见反馈";
            [self.navigationController pushViewController:feedVC animated:YES];
            break;
        case 1:
            //加符号说明的跳转
            feedVC.navigationItem.title  = @"加符号说明";
            
            [self.navigationController pushViewController:feedVC animated:YES];
            
            break;
        case 2:
            
            [QDBannerTools pp_BannerTitle:@" 修改密码成功" andTime:1];
            
            break;
        case 3:
            //清除缓存
            [self filePath];
            
            [self alertView];
            
            break;
            
        default:
            break;
    }
    

//    [self.navigationController pushViewController:feedVC animated:YES];

}

#pragma mark-清除缓存
// 显示缓存大小
-( float )filePath
{
    
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    return [ self folderSizeAtPath :cachPath];
    
}
//1:首先我们计算一下 单个文件的大小

- ( long long ) fileSizeAtPath:( NSString *) filePath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if ([manager fileExistsAtPath :filePath]){
        
        return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
    }
    
    return 0 ;
    
}
//2:遍历文件夹获得文件夹大小

- ( float ) folderSizeAtPath:( NSString *) folderPath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
    
    NSString * fileName;
    
    long long folderSize = 0 ;
    
    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
        
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
        
    }
    
    self.fileSize = folderSize/( 1024.0 * 1024.0 );
    return folderSize/( 1024.0 * 1024.0 );
    
}

// 清理缓存

- (void)clearFile
{
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
    
    NSLog ( @"cachpath = %@" , cachPath);
    
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        
        NSString * path = [cachPath stringByAppendingPathComponent :p];
        
        if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
            
            [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
            
        }
        
    }
    
    [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject : nil waitUntilDone : YES ];
    NSLog(@"%@",[NSThread currentThread]);
}
-(void)clearCachSuccess
{
    
}

- (void)alertView
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您确定要清理缓存？" message:[NSString stringWithFormat:@"缓存大小为%0.2fMB",self.fileSize] preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"清除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [QDBannerTools pp_BannerTitle:@"清理成功" andTime:0.7];
        
        //调用清理缓存
        [self clearFile];
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}


@end
